<template>
  <div class="v-bg ">
    <el-dialog
      title="登录"
      class="animated  fadeInUp"
      :visible="centerDialogVisible"
      width="30%"
      center
      :show-close="false"
      :modal="false"
    >
      <el-form
        ref="ruleForm"
        v-loading="loading"
        label-width="0px"
        label-position="left"
        :model="ruleForm"
        status-icon
        :rules="rules"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input v-model="ruleForm.username" type="text" auto-complete="off">
            <v-icon slot="prefix" name="icon-people" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="ruleForm.password" type="password" auto-complete="off">
            <v-icon slot="prefix" name="icon-unlock" />
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="width:100%" type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button type="text" @click="resetForm('ruleForm')">重置</el-button>
          <el-button type="text" style="float:right" @click="register('ruleForm')">注册</el-button>
        </el-form-item>
        <el-alert
          title="用户名:aaaa guest 密码:123 / 用户名:admin 密码:123"
          :closable="false"
          type="success"
        />
      </el-form>
    </el-dialog>
    <div id="particles-js" />
  </div>
</template>
<script>
export default {
  data () {
    var validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    var validateUsername = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请填写用户名'))
      } else {
        callback()
      }
    }
    return {
      loading: false,
      centerDialogVisible: true,
      ruleForm: {
        username: 'momo',
        password: '123456'
      },
      rules: {
        username: [{
          validator: validateUsername,
          trigger: 'blur'
        }],
        password: [{
          validator: validatePassword,
          trigger: 'blur'
        }]
      }
    }
  },
  computed: {},
  mounted () {
  },
  methods: {
    userLogin (res) {
      console.log(res)
    },
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          const sysUserLoginName = this.ruleForm.username
          const sysUserPwd = this.ruleForm.password
          const param = {
            sysUserPwd: sysUserPwd,
            sysUserLoginName: sysUserLoginName
          }
          this.$store.dispatch('user/login', param)
            .then(() => {
              this.$router.push({ path: this.redirect || '/index' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
          // this.$api.article.login(param).then(res => {
          //   console.log('this.$api.article.')
          //   console.log(res)
          // })
          // axios({
          //   method: 'post',
          //   url: 'http://127.0.0.1:8086/api/platform/sysMain/login/v1',
          //   data: {
          //     sysUserPwd: this.ruleForm.username,
          //     sysUserLoginName: this.ruleForm.password
          //   }
          // })
          //   .then(this.userLogin)
        } else {
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    register () {
      alert('开发中。。。')
    },
    beforeRouteLeave (to, from, next) {
      // 导航离开该组件的对应路由时调用
      // 可以访问组件实例 `this`
      console.log('关闭loading')
      this.loading = false
    }
  }
}
</script>
<style>
  .v-bg {
    height: 100%;
    min-height: 100vh;
    background-position: center;
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-image: url('../../assets/fullstack.jpg');
  }

  canvas {
    display: block;
    vertical-align: bottom;
  }

  /* ---- particles.js container ---- */
  #particles-js {
    position: absolute;
    width: 100%;
    height: 100%;
    background-color: none;
    background-image: url('');
    background-repeat: no-repeat;
    background-size: cover;
    background-position: 50% 50%;
  }

  /* ---- stats.js ---- */
  .count-particles {
    background: #000022;
    position: absolute;
    top: 48px;
    left: 0;
    width: 80px;
    color: #13e8e9;
    font-size: 0.8em;
    text-align: left;
    text-indent: 4px;
    line-height: 14px;
    padding-bottom: 2px;
    font-family: Helvetica, Arial, sans-serif;
    font-weight: bold;
  }

  .js-count-particles {
    font-size: 1.1em;
  }

  #stats,
  .count-particles {
    -webkit-user-select: none;
    margin-top: 5px;
    margin-left: 5px;
  }

  #stats {
    border-radius: 3px 3px 0 0;
    overflow: hidden;
  }

  .count-particles {
    border-radius: 0 0 3px 3px;
  }
</style>
